package org.theSeed.base.utils;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 反射工具
 */
public class SeedReflectUtil {
    /**
     * 获取类实现的所有接口（包括父类继承的接口）
     */
    public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
        Set<Class<?>> interfaces = new LinkedHashSet<>();
        collectAllInterfaces(clazz, interfaces);
        return interfaces;
    }







    private static void collectAllInterfaces(Class<?> clazz, Set<Class<?>> interfaces) {
        if (clazz == null || clazz == Object.class) {
            return;
        }

        // 获取当前类直接实现的接口
        Class<?>[] currentInterfaces = clazz.getInterfaces();
        for (Class<?> iface : currentInterfaces) {
            interfaces.add(iface);
            // 递归获取接口继承的父接口
            collectAllInterfacesFromInterface(iface, interfaces);
        }

        // 递归获取父类的接口
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null) {
            collectAllInterfaces(superClass, interfaces);
        }
    }

    private static void collectAllInterfacesFromInterface(Class<?> iface, Set<Class<?>> interfaces) {
        Class<?>[] parentInterfaces = iface.getInterfaces();
        for (Class<?> parentIface : parentInterfaces) {
            if (interfaces.add(parentIface)) {
                collectAllInterfacesFromInterface(parentIface, interfaces);
            }
        }
    }
}
